package cn.ccbom.bom.interview.util.script.task;

import cn.hutool.core.util.IdUtil;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;

@Data
@EqualsAndHashCode(callSuper = true)
public class Task extends AbstractRunStatus<String> {
    private String title;
    private List<TaskItem> taskItems;

    private Map<String, Object> initParams;
    private Map<String, Object> runParams;

    public Task(String title) {
        this.setId(IdUtil.fastSimpleUUID());
        this.title = title;
        this.setCreateTime(LocalDateTime.now());
        this.setStatus(RunStatus.CREATE);
        this.initParams = new HashMap<>();
    }

    public Task add(TaskItem taskItem) {
        if (this.taskItems == null) {
            this.taskItems = new LinkedList<>();
            this.runParams = new HashMap<>();
        }
        this.taskItems.add(taskItem);
        return this;
    }

    public String run(Executor executor) {
        this.setStatus(RunStatus.RUNNING);
        this.setStartTime(LocalDateTime.now());
        //executor.execute(()->{
        //CompletableFuture.allOf(
        taskItems.forEach(itme -> CompletableFuture.runAsync(() -> {
            //itme.run();//任务项
        }, executor));
        //).toArray(CompletableFuture[]::new)
        //  ).join();
        //this.setCost(LocalDateTimeUtil.between(getStartTime(), getEndTime(), ChronoUnit.MILLIS));
        //});

        return this.getId();
    }


}
